package com._51cto.healthy.controller;


import com._51cto.healthy.content.P;
import com._51cto.healthy.content.R;
import com._51cto.healthy.pojo.SetMeal;
import com._51cto.healthy.service.SetMealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "套餐管理", tags = "套餐管理")
@CrossOrigin
@RestController
@RequestMapping("/set-meal")
public class SetMealController {
    @DubboReference
    private SetMealService setMealService;


    @ApiOperation(value = "分页查询", notes = "套餐信息-分页获取", tags = "套餐管理")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    @GetMapping("/page/{pageSize}/{pageNum}")
    public R queryAll(@RequestParam(value = "query", required = false/*表示参数不是必填项*/) String query,
                      @PathVariable("pageNum") Integer pageNum,
                      @PathVariable("pageSize") Integer pageSize) {
        P<SetMeal> setMealP = setMealService.queryPageList(query, pageNum, pageSize);
        return R.success(setMealP);
    }

    @ApiOperation(value = "检测组Ids", notes = "套餐与检测组关联信息Ids", tags = "套餐管理")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @GetMapping("/group/{id}")
    public R queryItemIds(@PathVariable("id") Integer id){
        List<Integer> idS = setMealService.queryItemIds(id);
        return R.success(idS);
    }

    @ApiOperation(value = "套餐删除", notes = "套餐信息-主键删除，级联删除检测组关联信息", tags = "套餐管理")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @DeleteMapping("/{id}")
    public R deleteByPk(@PathVariable("id") Integer pk){
        setMealService.deleteByPK(pk);
        return R.success("删除成功");
    }

    @ApiOperation(value = "套餐新增", notes = "新增套餐信息and检测组关联信息", tags = "套餐管理")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @PostMapping()
    public R insert(@RequestBody SetMeal setMeal){
        setMealService.insert(setMeal);
        return R.success("添加成功");
    }

    @ApiOperation(value = "套餐编辑", notes = "编辑套餐信息and检测组关联信息", tags = "套餐管理")
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @PutMapping()
    public R update(@RequestBody SetMeal setMeal){
        setMealService.update(setMeal);
        return R.success("更新成功");
    }


}
